/* $Id: OverviewPanel.java 39 2009-07-24 22:48:06Z h_m_r@gmx.de $ */
package org.jugger.tournament.gui.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.TableModelListener;

import org.jugger.tournament.model.Round;
import org.jugger.tournament.model.TeamStats;
import org.jugger.tournament.model.Tournament;

import util.GUIUtils;
import util.I18N;

public class OverviewPanel extends JPanel {

	private Vector<TableModelListener> tableModelListeners = new Vector<TableModelListener>();
	private RoundTable roundTable;
	private RankingTable rankingTable;
	private JButton recalculateCurrentRoundButton;
	private JButton calculateNextRoundButton;

	public OverviewPanel(final Tournament tournament) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		recalculateCurrentRoundButton = new JButton(I18N.__("recalculateCurrentRound"));
		recalculateCurrentRoundButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (confirmRecalculate()) {
					tournament.recalculateCurrentRound();
					for (TableModelListener l : tableModelListeners) {
						l.tableChanged(null);
					}
				}
			}
		});
		calculateNextRoundButton = new JButton(I18N.__("calculateNextRound"));
		calculateNextRoundButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (confirmNextRound()) {
					tournament.calculateNextRound();
					for (TableModelListener l : tableModelListeners) {
						l.tableChanged(null);
					}
				}
			}
		});
		buildGUI(tournament);
		reloadData(tournament);
	}

	private boolean confirmRecalculate() {
		Object[] options = { I18N.__("dialogYes"), I18N.__("dialogNo") };
		return JOptionPane.YES_OPTION == JOptionPane.showOptionDialog(this, I18N
				.__("dialogTextRecalculateCurrentRound"), I18N
				.__("dialogTitleRecalculateCurrentRound"), JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
	}

	private boolean confirmNextRound() {
		Object[] options = { I18N.__("dialogYes"), I18N.__("dialogNo") };
		return JOptionPane.YES_OPTION == JOptionPane.showOptionDialog(this, I18N
				.__("dialogTextCalculateNewRound"), I18N.__("dialogTitleCalculateNewRound"),
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
	}

	public void buildGUI(Tournament tournament) {
		JPanel frame = GUIUtils.createTitledPanelY(I18N.__("numberedRound",
				new Object[] { tournament.countFinishedRounds() + 1 }) + " ("+I18N.__("currentRound")+")");

		Round round = tournament.getRound(tournament.countFinishedRounds() + 1);
		roundTable = new RoundTable(tournament, round, false);
		JPanel roundTablePanel = GUIUtils.createTitledPanelY(I18N.__("matches"));
		roundTablePanel.add(roundTable.getTableHeader());
		roundTablePanel.add(roundTable);
		frame.add(roundTablePanel);

		JPanel panel = GUIUtils.createXBoxLayoutPanel();
		panel.add(Box.createHorizontalGlue());
		panel.add(recalculateCurrentRoundButton);
		recalculateCurrentRoundButton.setEnabled(round.isRecalculatable());
		panel.add(calculateNextRoundButton);
		calculateNextRoundButton.setEnabled(round.isReadyForNextRound());
		frame.add(panel);

		Collection<TeamStats> rankingLastRound = tournament.countFinishedRounds() > 0 ? tournament.getRankingAfterRound(tournament.countFinishedRounds()) : null;
		rankingTable = new RankingTable(tournament, tournament.getCurrentRanking(), rankingLastRound, false);

		JPanel rankingTablePanel = GUIUtils.createTitledPanelY(I18N.__("ranking"));
		rankingTablePanel.add(rankingTable.getTableHeader());
		rankingTablePanel.add(rankingTable);
		frame.add(rankingTablePanel);

		add(frame);
	}

	public void reloadData(Tournament tournament) {

		removeAll();
		buildGUI(tournament);
		repaint();
		this.updateUI();

		Vector<TableModelListener> listeners = new Vector<TableModelListener>(tableModelListeners);
		tableModelListeners.clear();

		for (TableModelListener l : listeners) {
			addTableModelListener(l);
		}
	}

	private static final long serialVersionUID = 7986290518406485210L;

	public void addTableModelListener(TableModelListener listener) {
		rankingTable.getModel().addTableModelListener(listener);
		roundTable.getModel().addTableModelListener(listener);
		tableModelListeners.add(listener);
	}

}
